package cn.snailhd.core.base.config;

import java.nio.charset.Charset;
import java.util.List;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

import cn.snailhd.core.base.interceptor.RequestParamResovler;

@Configuration
public class MvcConfiguration extends WebMvcConfigurerAdapter {
	
	 	@Bean
	    public HttpMessageConverter<String> responseBodyConverter(){
	        StringHttpMessageConverter converter = new StringHttpMessageConverter(Charset.forName("UTF-8"));
	        return converter;
	    }
	    @Bean
	    public ObjectMapper getObjectMapper() {
	    	ObjectMapper objMapper = new ObjectMapper();
	    	objMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); //忽略未知属性
	        return objMapper;
	    }
	    @Bean
	    public MappingJackson2HttpMessageConverter messageConverter() {
	        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
	        converter.setObjectMapper(getObjectMapper());
	        return converter;
	    }
	    @Override
	    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
	        super.configureMessageConverters(converters);
	        //解决中文乱码
	        converters.add(responseBodyConverter());
	        //解决 添加解决中文乱码后 上述配置之后，返回json数据直接报错 500：no convertter for return value of type
	        converters.add(messageConverter());
	    }
	    
	    @Override
	    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
//	        super.addArgumentResolvers(argumentResolvers);
	        argumentResolvers.add(new RequestParamResovler());
	    }
	    
	    
}
